<?php
/**
 * @ClassName: DateTimeLogic
 * @Description: TODO
 * @author: gydtrade
 * @Date: 2025-11-20 11:13
 * @Version: V1.0.0
 */

namespace app\common\logic;

class DateTimeLogic
{
    //默认初始化起止时间
    protected $suffix_start = ' 00:00:00';
    protected $suffix_end = ' 23:59:59';

    /**
     * 处理起止时间
     * @param $type [-6：近半年，-5：去年，-51：近一年，-4：上季度，-41：近三月，-3：上月，-31：近一月，-2：上周，-21：近一周，-1：昨日，0：今日（默认），1：明日，2：本周，21：下周，22：未来七天，3：当月，31：下月，32：未来一月，4：本季度，41：下季度，5：本年，51：明年，52：近一年，6：近半年]
     * @return array|string[]
     */
    function handleStartStopDate($type = 0)
    {
        switch ($type) {
            case -6:
                $dateArray = [
                    'msg' => '近半年',
                    'startDate' => date("Y-m-d H:i:s", strtotime('-6 month')),
                    'endDate' => date("Y-m-d H:i:s")
                ];
                break;
            case -5:
                $dateArray = [
                    'msg' => '去年',
                    'startDate' => date('Y-01-01', strtotime('-1 year')) . $this->suffix_start,
                    'endDate' => date('Y-12-31', strtotime('-1 year')) . $this->suffix_end
                ];
                break;
            case -51:
                $dateArray = [
                    'msg' => '近一年',
                    'startDate' => date('Y-m-d H:i:s', strtotime('-1 year')),
                    'endDate' => date("Y-m-d H:i:s")
                ];
                break;
            case -4:
                $dateArray = $this->getLastQuarterTime();
                $dateArray['msg'] = '上季度';
                break;
            case -41:
                $dateArray = [
                    'msg' => '近三月',
                    'startDate' => date("Y-m-d H:i:s", strtotime('-3 month')),
                    'endDate' => date("Y-m-d H:i:s")
                ];
                break;
            case -3:
                $dateArray = [
                    'msg' => '上月',
                    'startDate' => date("Y-m-d H:i:s", mktime(0, 0, 0, date("m") - 1, 1, date("Y"))),
                    'endDate' => date("Y-m-d H:i:s", mktime(23, 59, 59, date("m"), 0, date("Y")))
                ];
                break;
            case -31:
                $dateArray = [
                    'msg' => '近一月',
                    'startDate' => date("Y-m-d H:i:s", strtotime('-1 month')),
                    'endDate' => date("Y-m-d H:i:s")
                ];
                break;
            case -2:
                $dateArray = [
                    'msg' => '上周',
                    'startDate' => date("Y-m-d H:i:s", mktime(0, 0, 0, date("m"), date("d") - date("w") + 1 - 7, date("Y"))),
                    'endDate' => date("Y-m-d H:i:s", mktime(23, 59, 59, date("m"), date("d") - date("w") + 7 - 7, date("Y")))
                ];
                break;
            case -21:
                $dateArray = [
                    'msg' => '近一周',
                    'startDate' => date('Y-m-d H:i:s', strtotime('-7 days')),
                    'endDate' => date('Y-m-d H:i:s')
                ];
                break;
            case -1:
                $dateArray = [
                    'msg' => '昨日',
                    'startDate' => date('Y-m-d', strtotime('-1 day')) . $this->suffix_start,
                    'endDate' => date('Y-m-d', strtotime('-1 day')) . $this->suffix_end
                ];
                break;
            case 1:
                $dateArray = [
                    'msg' => '明日',
                    'startDate' => date('Y-m-d', strtotime('1 day')) . $this->suffix_start,
                    'endDate' => date('Y-m-d', strtotime('1 day')) . $this->suffix_end
                ];
                break;
            case 2:
                $dateArray = $this->getWeekTime();
                $dateArray['msg'] = '本周';
                break;
            case 21:
                $dateArray = [
                    'msg' => '下周',
                    'startDate' => date('Y-m-d', strtotime('next week Monday')) . $this->suffix_start,
                    'endDate' => date('Y-m-d', strtotime('next week Sunday')) . $this->suffix_end
                ];
                break;
            case 22:
                $dateArray = [
                    'msg' => '未来七天',
                    'startDate' => date('Y-m-d H:i:s'),
                    'endDate' => date('Y-m-d H:i:s', strtotime('+7 day'))
                ];
                break;
            case 3:
                $dateArray = $this->getMonthTime();
                $dateArray['msg'] = '本月';
                break;
            case 31:
                $dateArray = [
                    'msg' => '下月',
                    'startDate' => date('Y-m-01', strtotime('1 month')) . $this->suffix_start,
                    'endDate' => date('Y-m-t', strtotime('1 month')) . $this->suffix_end
                ];
                break;
            case 32:
                $dateArray = [
                    'msg' => '未来一月',
                    'startDate' => date('Y-m-d H:i:s'),
                    'endDate' => date("Y-m-d H:i:s", strtotime('1 month'))
                ];
                break;
            case 4:
                $dateArray = $this->getQuarterTime();
                $dateArray['msg'] = '本季度';
                break;
            case 41:
                $dateArray = $this->getNextQuarterTime();
                $dateArray['msg'] = '下季度';
                break;
            case 42:
                $dateArray = [
                    'msg' => '未来三月',
                    'startDate' => date('Y-m-d H:i:s'),
                    'endDate' => date("Y-m-d H:i:s", strtotime('+3 month'))
                ];
                break;
            case 5:
                $dateArray = $this->getYearTime();
                $dateArray['msg'] = '本年';
                break;
            case 51:
                $dateArray = [
                    'msg' => '明年',
                    'startDate' => date('Y-01-01', strtotime('1 year')) . $this->suffix_start,
                    'endDate' => date('Y-12-31', strtotime('1 year')) . $this->suffix_end
                ];
                break;
            case 52:
                $dateArray = [
                    'msg' => '未来一年',
                    'startDate' => date('Y-m-d H:i:s'),
                    'endDate' => date("Y-m-d H:i:s", strtotime('1 year'))
                ];
                break;
            case 6:
                $dateArray = [
                    'msg' => '未来半年',
                    'startDate' => date('Y-m-d H:i:s'),
                    'endDate' => date("Y-m-d H:i:s", strtotime('+6 month'))
                ];
                break;
            default:
                $dateArray = [
                    'msg' => '今日',
                    'startDate' => date('Y-m-d') . $this->suffix_start,
                    'endDate' => date('Y-m-d') . $this->suffix_end
                ];
                break;
        }

        return $dateArray;
    }

    /**
     * 获取本周起止时间
     * @return string[]
     */
    function getWeekTime()
    {
        // 获取当前日期
        $currentDate = date('Y-m-d');
        // 获取本周起始日期
        $weekStart = date('Y-m-d', strtotime('this week', strtotime($currentDate)));
        // 获取本周结束日期
        $weekEnd = date('Y-m-d', strtotime('this week +6 days', strtotime($currentDate)));
        $start_time = $weekStart . $this->suffix_start;
        $end_time = $weekEnd . $this->suffix_end;
        return $dateArray = [
            'startDate' => $start_time,
            'endDate' => $end_time
        ];
    }

    /**
     * 获取本月起止时间
     * @return string[]
     */
    function getMonthTime()
    {
        // 获取当前日期
        $currentDate = date('Y-m-d');
        // 获取本月起始日期
        $monthStart = date('Y-m-01', strtotime($currentDate));
        // 获取本月结束日期
        $monthEnd = date('Y-m-t', strtotime($currentDate));
        $start_time = $monthStart . $this->suffix_start;
        $end_time = $monthEnd . $this->suffix_end;
        return $dateArray = [
            'startDate' => $start_time,
            'endDate' => $end_time
        ];
    }

    /**
     * 获取本季度起止时间
     * @return array
     */
    function getQuarterTime()
    {
        $season = ceil((date('n')) / 3);//当月是第几季度
        $start = date('Y-m-d H:i:s', mktime(0, 0, 0, $season * 3 - 3 + 1, 1, date('Y')));
        $end = date('Y-m-d H:i:s', mktime(23, 59, 59, $season * 3, date('t', mktime(0, 0, 0, $season * 3, 1, date("Y"))), date('Y')));
        return $dateArray = [
            'startDate' => $start,
            'endDate' => $end
        ];
    }

    /**
     * 获取上季度起止时间
     * @return array
     */
    function getLastQuarterTime()
    {
        $season = ceil((date('n')) / 3);//当月是第几季度
        $season -= 1;//上一季度
        $start = date('Y-m-d H:i:s', mktime(0, 0, 0, $season * 3 - 3 + 1, 1, date('Y')));
        $end = date('Y-m-d H:i:s', mktime(23, 59, 59, $season * 3, date('t', mktime(0, 0, 0, $season * 3, 1, date("Y"))), date('Y')));
        return $dateArray = [
            'startDate' => $start,
            'endDate' => $end
        ];
    }

    /**
     * 获取下季度起止时间
     * @return array
     */
    function getNextQuarterTime()
    {
        $season = ceil((date('n')) / 3);//当月是第几季度
        $season += 1;//下一季度
        $start = date('Y-m-d H:i:s', mktime(0, 0, 0, $season * 3 - 3 + 1, 1, date('Y')));
        $end = date('Y-m-d H:i:s', mktime(23, 59, 59, $season * 3, date('t', mktime(0, 0, 0, $season * 3, 1, date("Y"))), date('Y')));
        return $dateArray = [
            'startDate' => $start,
            'endDate' => $end
        ];
    }

    /**
     * 获取本年起止时间
     * @return array
     */
    function getYearTime()
    {
        $currentYear = date('Y');
        // 获取本年度起始时间
        $dateArray['startDate'] = date('Y-01-01', strtotime($currentYear)) . $this->suffix_start;
        // 获取本年度结束时间
        $dateArray['endDate'] = date('Y-12-31', strtotime($currentYear)) . $this->suffix_end;
        return $dateArray;
    }

}